<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    width="30%"
    @close="close"
  >
    <el-form ref="form" :model="formModel" label-width="120px" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input id="name" v-model="formModel.name" style="width: 80%" size="small" placeholder="2-10个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input id="code" v-model="formModel.code" style="width: 80%" size="small" placeholder="2-10个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <el-select id="managerId" v-model="formModel.managerId" size="small" style="width:80%" placeholder="请选择负责人">
          <el-option
            v-for="item in managerList"
            :key="item.id"
            :label="item.username"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          id="introduce"
          v-model="formModel.introduce"
          style="width: 80%"
          type="textarea"
          :rows="4"
          placeholder="1-100个字符"
        />
      </el-form-item>
      <el-form-item>
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-button size="mini" type="primary" @click="handleDept">提交</el-button>
            <el-button size="mini" @click="close">取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { getDeptsList, getManagerList, addDept, getDeptDetail, updateDeptDetail } from '@/api/department'
export default {
  components: {},
  data() {
    return {
      title: '',
      dialogVisible: false,
      formModel: {
        code: '',
        introduce: '',
        managerId: '',
        name: '',
        pid: ''
      },
      managerList: [],
      depatDetail: {},
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 2, max: 10, message: '部门名称应该为2-10个字符', trigger: 'blur' },
          { validator: async(rule, val, callback) => {
            let res = await getDeptsList()
            // 如果是编辑模式下,要先移除自身这一项数据,否则表单失去焦点就报错
            if (this.formModel.id) {
              res = res.filter(item => item.id !== this.formModel.id)
            }
            if (res.some(item => item.name === val)) {
              callback(new Error('部门名称不能重复'))
            } else {
              callback()
            }
          }, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { min: 2, max: 10, message: '部门编码应该为2-10个字符', trigger: 'blur' },
          { validator: async(rule, val, callback) => {
            let res = await getDeptsList()
            // 如果是编辑模式下,要先移除自身这一项数据,否则表单失去焦点就报错
            if (this.formModel.id) {
              res = res.filter(item => item.id !== this.formModel.id)
            }
            if (res.some(item => item.code === val)) {
              callback(new Error('部门编码不能重复'))
            } else {
              callback()
            }
          },
          trigger: 'blur' }
        ],
        managerId: { required: true, message: '请选择负责人', trigger: 'blur' },
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' },
          { min: 2, max: 10, message: '部门介绍应该为1-100个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getgetManagerList()
  },
  methods: {
    async open(title, id) {
      this.title = title
      if (title === '编辑部门') {
        await this.getDeptDetail(id)
      } else {
        this.formModel.pid = id
      }
      this.dialogVisible = true
    },
    // 获取管理员列表
    async getgetManagerList() {
      this.managerList = await getManagerList()
    },
    // 提交表单
    handleDept() {
      this.$refs.form.validate(async(valid) => {
        if (valid) {
          if (this.title === '新增部门') {
            await addDept(this.formModel)
          } else {
            await updateDeptDetail(this.formModel)
          }
          this.close()
          this.$message.success(`${this.title}成功`)
          this.$emit('handlerDept')
        }
      })
    },
    // 获取部门详情
    async getDeptDetail(id) {
      this.formModel = await getDeptDetail(id)
    },
    close() {
      this.dialogVisible = false
      this.reset()
      this.$refs.form.resetFields()
    },
    reset() {
      // 重置数据避免编辑数据时影响到添加数据
      this.formModel = {
        code: '',
        introduce: '',
        managerId: '',
        name: '',
        pid: ''
      }
    }
  }
}
</script>

<style scoped>

</style>

